//
//  INspectDatilViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/10.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "INspectDatilViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "InspectDatil.h"
#import "dataBaseHandle.h"
#import "InspectDatil.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface INspectDatilViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property(nonatomic,retain)UIWebView *myWeb;
@property(nonatomic,retain)UIImageView *myView;
@property(nonatomic,retain)UIView *smView;
@property(nonatomic,retain)InspectDatil *insd;
@property(nonatomic,assign)BOOL isclick;
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,assign)NSInteger num;
@property(nonatomic,retain)UIButton *top;
@end

@implementation INspectDatilViewController
- (void)dealloc
{
    [_smView release];
    [_myView release];
    [_myWeb release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.isclick = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatWebView];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myWeb.scrollView.contentInset = UIEdgeInsetsMake(290, 0, 0, 0);
    self.myView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -290, WIDTH, 280)];

    self.myWeb.scrollView.pagingEnabled = YES;
    [self PleaseData];
    
    //创建右按钮
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(0.85 * WIDTH, 20, 40, 40);
    [self.button setTitle:@"" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(clickShare:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiala"] forState:UIControlStateNormal];
    [self.navigationController.view addSubview:self.button];
    
    //创建隐藏试图
    self.smView = [[UIView alloc]initWithFrame:CGRectMake(0, -60, WIDTH, 60)];
    [self.myWeb.scrollView addSubview:self.smView];
    self.smView.layer.borderWidth = 1;
    self.smView.layer.cornerRadius = 8;
    self.smView.layer.masksToBounds = YES;
    self.smView.layer.borderColor = [UIColor clearColor].CGColor;
    self.smView.backgroundColor = [UIColor whiteColor];
    self.smView.alpha = 0.8;
    [_smView release];
    
    
    //创建返回顶部按钮
    self.top = [UIButton buttonWithType:UIButtonTypeCustom];
    self.top.frame = CGRectMake(0.8 * WIDTH, 0.65 *HEIGHT, 40, 40);
    [self.top setTitle:@"" forState:UIControlStateNormal];
    [self.top addTarget:self action:@selector(clickToTop:) forControlEvents:UIControlEventTouchUpInside];
    [self.myWeb addSubview:self.top];
    [self.top setBackgroundImage:[UIImage imageNamed:@"iconfont-iconfontxiangxiajiantoujian"] forState:UIControlStateNormal];
    self.top.alpha = 0.43;
}
//创建详情view
-(void)creatWebView
{
    self.myWeb = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
    self.myWeb.backgroundColor = [UIColor whiteColor];
    self.myWeb.delegate = self;
    [self.view addSubview:self.myWeb];
    [_myWeb release];
}
//请求数据
-(void)PleaseData
{
    NSString *url = [NSString stringWithFormat:@"http://api.yi18.net/check/show?id=%ld",(long)self.id];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *Dic = [dic objectForKey:@"yi18"];
        InspectDatil *ind = [[InspectDatil alloc]init];
        [ind setValuesForKeysWithDictionary:Dic];
        self.insd = ind;
        //图片
        NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",self.insd.img];
        NSURL *url = [NSURL URLWithString:str];
        [self.myView sd_setImageWithURL:url];
        [self.myWeb loadHTMLString:self.insd.detailText baseURL:nil];
        [self.myWeb.scrollView addSubview:self.myView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
}

//下拉箭头点击方法
-(void)clickShare:(UIButton *)bu
{
    self.button.transform = CGAffineTransformRotate(self.button.transform, M_PI);
    if (self.isclick == NO) {
        
        self.smView = [[UIView alloc]initWithFrame:CGRectMake(0, -60, WIDTH, 60)];
        self.smView.backgroundColor = [UIColor whiteColor];
        self.smView.alpha = 0.76;
        [self.view addSubview:self.smView];
        [_smView release];
        [UIView animateWithDuration:1.2 animations:^{
            self.smView.frame = CGRectMake(0, 0, WIDTH, 60);
            
        }];
        
        
        UIButton *bu1 = [UIButton buttonWithType:UIButtonTypeSystem];
        bu1.frame = CGRectMake(0.28 *WIDTH, 10, 40, 40);
        [bu1 setBackgroundImage:[UIImage imageNamed:@"iconfont-701"] forState:UIControlStateNormal];
        [bu1 setTitle:@"" forState:UIControlStateNormal];
        [bu1 addTarget:self action:@selector(clickBu1:) forControlEvents:UIControlEventTouchUpInside];
        [self.smView addSubview:bu1];
        
        UIButton *bu2 = [UIButton buttonWithType:UIButtonTypeSystem];
        bu2.frame = CGRectMake(0.63 *WIDTH, 10, 40, 40);
        [bu2 setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
        [bu2 setTitle:@"" forState:UIControlStateNormal];
        [bu2 addTarget:self action:@selector(clickToBu2:) forControlEvents:UIControlEventTouchUpInside];
        [self.smView addSubview:bu2];
        self.isclick = YES;
    }else if (self.isclick == YES){
        [UIView animateWithDuration:1.2 animations:^{
            self.smView.frame = CGRectMake(0, -60, WIDTH, 60);
            
        }];
        //        self.smallview.hidden = YES;
        self.isclick = NO;
    }
}
//收藏按钮
-(void)clickBu1:(UIButton *)bu
{
    [[dataBaseHandle shareDB]openDB];
    
    [[dataBaseHandle shareDB]creatInspectTable];
    
    NSMutableArray *arr = [[dataBaseHandle shareDB]selectInspectTable];
    
    if (arr.count > 0) {
        for (InspectDatil *ind in arr) {
            if ([ind.title isEqual:self.insd.name]) {
                UIAlertView *alear = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                self.num = 1;
                [alear show];
                [alear release];
                break;
            }
        }
        if (self.num != 1) {
            [[dataBaseHandle shareDB]insertInspectthWithTitle:self.insd.name imgUrl:[NSString stringWithFormat:@"http://www.yi18.net/%@",self.insd.img] id:self.insd.id];
            UIAlertView *alear = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alear show];
            [alear release];
        }
    }
    if (arr.count == 0) {
        [[dataBaseHandle shareDB]insertInspectthWithTitle:self.insd.name imgUrl:[NSString stringWithFormat:@"http://www.yi18.net/%@",self.insd.img] id:self.insd.id];
        NSLog(@"收藏");
        UIAlertView *alear = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alear show];
        [alear release];
    }
}
//分享按钮
-(void)clickToBu2:(UIButton *)bu
{
    NSString *image = [NSString stringWithFormat:@"http://www.yi18.net/%@",self.insd.img];
    NSString *text = self.insd.name;
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55406a3ce0f55a7bd5002d90" shareText:text shareImage:image shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, nil] delegate:self];
    
}

//navigationController.view 隐藏问题
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.button.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.button.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}

//返回顶部
-(void)clickToTop:(UIButton *)bu
{
    if (self.isclick == NO) {
        self.top.alpha = 0.8;
        self.isclick = YES;
    }
    [self.myWeb.scrollView setContentOffset:CGPointMake(0, -290) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
